(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:GzhlabelCtrl
     * @description
     * # GzhlabelCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('GzhlabelCtrl', GzhlabelCtrl);

    GzhlabelCtrl.$inject = ['$scope', 'dataService', '$location', 'helper'];

    function GzhlabelCtrl($scope, dataService, $location, helper) {

        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getlist(1);
        }

        $scope.wxgzhcallback = function(wxappid) {
            $scope.wxappid = wxappid;
            $scope.getlist(1);
        }

        $scope.getlist = function(pageno) {
            if ($scope.wxappid) {
                dataService.wxTagsGet($scope.wxappid).then(function(list) {
                    if (list.tags) {
                        $scope.label = list.tags;
                    }
                });
            } else {
                console.log("app_id不能为空！");
            }
        }


        $scope.getOne = function() {
            var query = { openid: $scope.openid };
            console.log("query", query);
            dataService.phpWxgzhmemberGet(query).then(function(list) {
                if (list.list) {
                    $scope.member = list.list[0];
                }
            });
        }

        $scope.addAction = function() {
            var data = $location.search().data;
            $scope.wxappid = $location.search().wxappid;
            if (data) {
                $scope.gzhlabel = JSON.parse(data) ;
            } else {
                $scope.gzhlabel = {};
            }
            console.error("$scope.gzhlabel",$scope.gzhlabel);
        }

        
        $scope.saveData = function() {
            var data = { "tag": { id: $scope.gzhlabel.id ,"name": $scope.gzhlabel.name } };
            var api = $scope.gzhlabel.id ? "wxTagsUpdate" : "wxTagsCreate";
            $scope.wxappid = $location.search().wxapp_id ? $location.search().wxapp_id:$scope.wxappid;
            dataService[api]($scope.wxappid, data).then(function(list) {
                if (list.tag) {
                    helper.myAlert("success", "#/gzhlabel/list");
                }
                else if(list.errmsg == "ok"){
                    helper.myAlert("success", "#/gzhlabel/list");
                }else{
                    helper.myAlert("error:"+list.errmsg)
                }
            });
        }

    }
})();